package com.example.cache_redis.Service.ServiceIml;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cache_redis.Dao.BookDao;
import com.example.cache_redis.Entity.Book;
import com.example.cache_redis.Service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

@Service
public class BookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {

    private Map<Integer,Book> cache =new HashMap<>();


    @Autowired
    BookDao bookDao;


    //缓存机制的简单思维逻辑

    public Book getUserById(Integer id) {
        Book book1 = cache.get(id);
        if (book1 ==null) {
            Book book = bookDao.selectById(id);
            cache.put(id,book);//将数据存入缓存
            return book;
        }
        return book1;
    }


    @Override
    public Page<Book> getPage(int currentPage, int pageSize) {
        Page<Book> bookPage = new Page<>(currentPage, pageSize);
        bookDao.selectPage(bookPage,null);
        return bookPage;

    }
}
